package com.hl.ruoyicsmshop.controller;

import java.util.List;
import java.util.Arrays;
import java.util.Map;
import java.util.HashMap;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.hl.ruoyicsmshop.domain.DoorsillActivity;
import com.hl.ruoyicsmshop.domain.DoorsillActivityGoods;
import com.hl.ruoyicsmshop.service.IDoorsillActivityService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 满额活动Controller
 * 
 * @author ruoyi
 * @date 2025-08-06
 */
@RestController
@RequestMapping("/doorsillActivity")
public class DoorsillActivityController extends BaseController
{
    @Autowired
    private IDoorsillActivityService doorsillActivityService;

    /**
     * 查询满额活动列表
     */
    @RequiresPermissions("csmshop:doorsillActivity:list")
    @GetMapping("/list")
    public TableDataInfo list(DoorsillActivity doorsillActivity)
    {
        startPage();
        List<DoorsillActivity> list = doorsillActivityService.selectDoorsillActivityList(doorsillActivity);
        return getDataTable(list);
    }

    /**
     * 查询进行中的满额活动列表
     */
    @GetMapping("/active/list")
    public AjaxResult getActiveList()
    {
        List<DoorsillActivity> list = doorsillActivityService.selectActiveDoorsillActivityList();
        return success(list);
    }

    /**
     * 根据商家ID查询满额活动
     */
    @GetMapping("/business/{businessId}")
    public AjaxResult getByBusinessId(@PathVariable("businessId") Integer businessId)
    {
        List<DoorsillActivity> list = doorsillActivityService.selectDoorsillActivityByBusinessId(businessId);
        return success(list);
    }

    /**
     * 导出满额活动列表
     */
    @RequiresPermissions("csmshop:doorsillActivity:export")
    @Log(title = "满额活动", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DoorsillActivity doorsillActivity)
    {
        List<DoorsillActivity> list = doorsillActivityService.selectDoorsillActivityList(doorsillActivity);
        ExcelUtil<DoorsillActivity> util = new ExcelUtil<DoorsillActivity>(DoorsillActivity.class);
        util.exportExcel(response, list, "满额活动数据");
    }

    /**
     * 获取满额活动详细信息
     */
    @RequiresPermissions("csmshop:doorsillActivity:query")
    @GetMapping(value = "/{daId}")
    public AjaxResult getInfo(@PathVariable("daId") Integer daId)
    {
        DoorsillActivity doorsillActivity = doorsillActivityService.selectDoorsillActivityByDaId(daId);
        if (doorsillActivity != null) {
            // 查询活动商品列表
            List<DoorsillActivityGoods> goodsList = doorsillActivityService.selectDoorsillActivityGoodsByDaId(daId);
            
            // 创建包含活动信息和商品列表的响应对象
            Map<String, Object> result = new HashMap<>();
            result.put("doorsillActivity", doorsillActivity);
            result.put("goodsList", goodsList);
            
            return success(result);
        }
        return error("满额活动不存在");
    }

    /**
     * 新增满额活动
     */
    @RequiresPermissions("csmshop:doorsillActivity:add")
    @Log(title = "满额活动", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DoorsillActivity doorsillActivity)
    {
        // 这里需要前端传递商品列表，可以通过请求体中的其他字段或者单独的接口
        // 暂时简化处理，不包含商品列表
        List<DoorsillActivityGoods> goodsList = null;
        return toAjax(doorsillActivityService.insertDoorsillActivity(doorsillActivity, goodsList));
    }

    /**
     * 新增满额活动（包含商品列表）
     */
    @RequiresPermissions("csmshop:doorsillActivity:add")
    @Log(title = "满额活动", businessType = BusinessType.INSERT)
    @PostMapping("/withGoods")
    public AjaxResult addWithGoods(@RequestBody DoorsillActivityRequest request)
    {
        return toAjax(doorsillActivityService.insertDoorsillActivity(request.getDoorsillActivity(), request.getGoodsList()));
    }

    /**
     * 修改满额活动
     */
    @RequiresPermissions("csmshop:doorsillActivity:edit")
    @Log(title = "满额活动", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DoorsillActivity doorsillActivity)
    {
        // 暂时简化处理，不包含商品列表
        List<DoorsillActivityGoods> goodsList = null;
        return toAjax(doorsillActivityService.updateDoorsillActivity(doorsillActivity, goodsList));
    }

    /**
     * 修改满额活动（包含商品列表）
     */
    @RequiresPermissions("csmshop:doorsillActivity:edit")
    @Log(title = "满额活动", businessType = BusinessType.UPDATE)
    @PutMapping("/withGoods")
    public AjaxResult editWithGoods(@RequestBody DoorsillActivityRequest request)
    {
        return toAjax(doorsillActivityService.updateDoorsillActivity(request.getDoorsillActivity(), request.getGoodsList()));
    }

    /**
     * 删除满额活动
     */
    @RequiresPermissions("csmshop:doorsillActivity:remove")
    @Log(title = "满额活动", businessType = BusinessType.DELETE)
    @DeleteMapping("/{daIds}")
    public AjaxResult remove(@PathVariable Integer[] daIds)
    {
        return toAjax(doorsillActivityService.deleteDoorsillActivityByDaIds(daIds));
    }

    /**
     * 更新满额活动状态
     */
    @RequiresPermissions("csmshop:doorsillActivity:edit")
    @Log(title = "满额活动状态更新", businessType = BusinessType.UPDATE)
    @PutMapping("/status/{daId}/{daStatus}")
    public AjaxResult updateStatus(@PathVariable("daId") Integer daId, @PathVariable("daStatus") Integer daStatus)
    {
        return toAjax(doorsillActivityService.updateDoorsillActivityStatus(daId, daStatus));
    }

    /**
     * 查询满额活动商品列表
     */
    @GetMapping("/goods/{daId}")
    public AjaxResult getGoodsList(@PathVariable("daId") Integer daId)
    {
        List<DoorsillActivityGoods> list = doorsillActivityService.selectDoorsillActivityGoodsByDaId(daId);
        return success(list);
    }

    /**
     * 根据商品ID查询满额活动
     */
    @GetMapping("/goods/activity/{goodsId}")
    public AjaxResult getActivityByGoodsId(@PathVariable("goodsId") Integer goodsId)
    {
        List<DoorsillActivityGoods> list = doorsillActivityService.selectDoorsillActivityGoodsByGoodsId(goodsId);
        return success(list);
    }

    /**
     * 检查商品是否参与满额活动
     */
    @GetMapping("/goods/check/{goodsId}")
    public AjaxResult checkGoodsInActivity(@PathVariable("goodsId") Integer goodsId)
    {
        boolean result = doorsillActivityService.checkGoodsInDoorsillActivity(goodsId);
        return success(result);
    }

    /**
     * 计算满额优惠金额
     */
    @PostMapping("/calculateDiscount")
    public AjaxResult calculateDiscount(@RequestParam Double totalAmount, @RequestBody List<Integer> goodsIds)
    {
        Double discount = doorsillActivityService.calculateDoorsillDiscount(totalAmount, goodsIds);
        return success(discount);
    }

    /**
     * 满额活动请求对象（包含活动信息和商品列表）
     */
    public static class DoorsillActivityRequest {
        private DoorsillActivity doorsillActivity;
        private List<DoorsillActivityGoods> goodsList;

        public DoorsillActivity getDoorsillActivity() {
            return doorsillActivity;
        }

        public void setDoorsillActivity(DoorsillActivity doorsillActivity) {
            this.doorsillActivity = doorsillActivity;
        }

        public List<DoorsillActivityGoods> getGoodsList() {
            return goodsList;
        }

        public void setGoodsList(List<DoorsillActivityGoods> goodsList) {
            this.goodsList = goodsList;
        }
    }
}
